<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>一言 | 每日一句</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #6c63ff;
            --secondary: #ff6584;
            --text: #333;
            --text-secondary: #666;
            --bg: #f8f9fa;
            --card-bg: #ffffff;
            --card-shadow: rgba(0, 0, 0, 0.1);
            --modal-bg: rgba(0, 0, 0, 0.5);
            --modal-content-bg: #ffffff;
            --divider: rgba(0, 0, 0, 0.1);
            --success: #4caf50;
            --border-radius: 16px;
            --transition: all 0.4s ease;
            --error: #e74c3c;
        }

        .dark-theme {
            --primary: #7a73ff;
            --secondary: #ff7b94;
            --text: #f0f0f0;
            --text-secondary: #aaa;
            --bg: #121212;
            --card-bg: #1e1e1e;
            --card-shadow: rgba(0, 0, 0, 0.4);
            --modal-content-bg: #2a2a2a;
            --divider: rgba(255, 255, 255, 0.1);
            --error: #e74c3c;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Serif SC', serif;
            background-color: var(--bg);
            color: var(--text);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
            transition: var(--transition);
            line-height: 1.6;
        }

        .container {
            width: 100%;
            max-width: 700px;
            text-align: center;
        }

        header {
            margin-bottom: 30px;
            position: relative;
        }

        h1 {
            font-size: 2.5rem;
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            margin-bottom: 10px;
            letter-spacing: 1px;
        }

        .subtitle {
            font-size: 1.1rem;
            color: var(--text-secondary);
            font-weight: 400;
        }

        .cors-notice {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 12px;
            margin: 0 auto 25px;
            max-width: 600px;
            border-left: 4px solid var(--primary);
            font-size: 0.95rem;
            text-align: left;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
        }

        .cors-notice.dark-theme {
            background: #1e1e1e;
            border-left: 4px solid var(--secondary);
        }

        .cors-notice h3 {
            margin-bottom: 8px;
            color: var(--primary);
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .card {
            background-color: var(--card-bg);
            border-radius: var(--border-radius);
            padding: 40px 30px;
            box-shadow: 0 10px 30px var(--card-shadow);
            margin-bottom: 25px;
            position: relative;
            overflow: hidden;
            transition: var(--transition);
            border-left: 5px solid var(--primary);
        }

        .hitokoto-text {
            font-size: 1.7rem;
            line-height: 1.8;
            margin-bottom: 25px;
            position: relative;
            padding: 10px 0;
            font-weight: 500;
            transition: var(--transition);
        }

        .hitokoto-text.error {
            color: var(--error);
            font-size: 1.4rem;
        }

        .hitokoto-text::before, 
        .hitokoto-text::after {
            content: """;
            font-size: 3rem;
            color: var(--primary);
            opacity: 0.2;
            position: absolute;
            font-family: Arial, sans-serif;
        }

        .hitokoto-text::before {
            top: -15px;
            left: -5px;
        }

        .hitokoto-text::after {
            content: """;
            bottom: -30px;
            right: -5px;
            transform: rotate(180deg);
        }

        .hitokoto-details {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
            margin-top: 15px;
        }

        .hitokoto-badge {
            padding: 5px 15px;
            background: rgba(108, 99, 255, 0.1);
            color: var(--primary);
            border-radius: 50px;
            font-size: 0.9rem;
        }

        .hitokoto-source {
            font-size: 1.05rem;
            color: var(--text-secondary);
            font-style: italic;
        }

        .buttons {
            display: flex;
            justify-content: center;
            gap: 15px;
            flex-wrap: wrap;
            margin-top: 10px;
        }

        .btn {
            background-color: var(--primary);
            color: white;
            border: none;
            padding: 14px 28px;
            font-size: 1.1rem;
            border-radius: 50px;
            cursor: pointer;
            transition: var(--transition);
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            flex: 1;
            min-width: 150px;
        }

        .btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 7px 14px rgba(108, 99, 255, 0.25);
        }

        .btn:active {
            transform: translateY(1px);
        }

        .btn-outline {
            background-color: transparent;
            border: 2px solid var(--primary);
            color: var(--primary);
        }

        .btn-outline:hover {
            background-color: var(--primary);
            color: white;
        }

        .settings-btn {
            position: absolute;
            top: 20px;
            right: 20px;
            background: transparent;
            border: none;
            font-size: 1.3rem;
            color: var(--text);
            cursor: pointer;
            opacity: 0.7;
            transition: var(--transition);
            padding: 8px;
            z-index: 20;
        }

        .settings-btn:hover {
            opacity: 1;
            transform: rotate(30deg);
            color: var(--primary);
        }

        .theme-toggle {
            position: absolute;
            top: 20px;
            left: 20px;
            background: transparent;
            border: none;
            font-size: 1.5rem;
            color: var(--text);
            cursor: pointer;
            opacity: 0.7;
            transition: var(--transition);
            padding: 8px;
            z-index: 20;
        }

        .theme-toggle:hover {
            opacity: 1;
            transform: rotate(15deg);
            color: var(--secondary);
        }

        .loader {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }

        .loader-dot {
            width: 12px;
            height: 12px;
            background-color: var(--primary);
            border-radius: 50%;
            margin: 0 5px;
            animation: loader-animation 1.5s infinite;
        }

        .loader-dot:nth-child(2) {
            animation-delay: 0.3s;
        }

        .loader-dot:nth-child(3) {
            animation-delay: 0.6s;
        }

        footer {
            margin-top: 20px;
            font-size: 0.9rem;
            color: var(--text-secondary);
            padding: 10px;
            width: 100%;
            max-width: 700px;
        }

        footer a {
            color: var(--primary);
            text-decoration: none;
            transition: var(--transition);
        }

        footer a:hover {
            text-decoration: underline;
            color: var(--secondary);
        }

        @media (max-width: 768px) {
            .card {
                padding: 25px 20px;
            }
            
            h1 {
                font-size: 2.2rem;
            }
            
            .hitokoto-text {
                font-size: 1.4rem;
            }
            
            .hitokoto-text.error {
                font-size: 1.2rem;
            }
            
            .hitokoto-text::before {
                left: -5px;
            }
            
            .hitokoto-text::after {
                right: -5px;
            }
            
            .buttons {
                flex-direction: column;
            }
            
            .btn {
                min-width: 100%;
            }

            .cors-notice {
                padding: 12px;
                font-size: 0.9rem;
            }
        }

        @keyframes loader-animation {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-15px); }
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: var(--modal-bg);
            z-index: 100;
            justify-content: center;
            align-items: center;
            backdrop-filter: blur(5px);
        }

        .modal-content {
            background-color: var(--modal-content-bg);
            border-radius: var(--border-radius);
            padding: 30px;
            width: 90%;
            max-width: 600px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
            max-height: 90vh;
            overflow-y: auto;
            animation: modalShow 0.4s ease;
        }

        @keyframes modalShow {
            from { opacity: 0; transform: translateY(-40px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid var(--divider);
        }

        .modal-title {
            font-size: 1.8rem;
            color: var(--text);
            font-weight: 700;
        }

        .close-modal {
            background: none;
            border: none;
            font-size: 1.5rem;
            color: var(--text-secondary);
            cursor: pointer;
            transition: var(--transition);
        }

        .close-modal:hover {
            color: var(--secondary);
            transform: scale(1.1);
        }

        .category-options {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
            gap: 12px;
            margin-top: 20px;
        }

        .category-option {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 15px 10px;
            background-color: rgba(108, 99, 255, 0.08);
            border-radius: 12px;
            cursor: pointer;
            transition: var(--transition);
            border: 2px solid transparent;
        }

        .category-option:hover {
            background-color: rgba(108, 99, 255, 0.15);
            transform: translateY(-3px);
        }

        .category-option.active {
            background-color: rgba(108, 99, 255, 0.15);
            border: 2px solid var(--primary);
            box-shadow: 0 5px 10px rgba(108, 99, 255, 0.2);
        }

        .category-icon {
            font-size: 1.8rem;
            margin-bottom: 10px;
            color: var(--primary);
        }

        .category-label {
            font-weight: 500;
            color: var(--text);
            font-size: 1rem;
        }

        .save-settings {
            width: 100%;
            margin-top: 25px;
            padding: 14px;
            font-size: 1.1rem;
            border-radius: 50px;
            background-color: var(--primary);
            color: white;
            border: none;
            cursor: pointer;
            font-weight: 600;
            transition: var(--transition);
            font-family: 'Noto Serif SC', serif;
        }

        .save-settings:hover {
            background-color: #5c55e0;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(108, 99, 255, 0.4);
        }

        .animated {
            animation: fadeIn 0.8s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .heart {
            color: var(--secondary);
            animation: heartbeat 1.5s infinite;
            margin: 0 3px;
        }

        @keyframes heartbeat {
            0% { transform: scale(1); }
            5% { transform: scale(1.1); }
            10% { transform: scale(1); }
            15% { transform: scale(1.2); }
            50% { transform: scale(1); }
            100% { transform: scale(1); }
        }

        .current-category-display {
            background: var(--primary);
            color: white;
            padding: 4px 12px;
            border-radius: 50px;
            font-size: 0.9rem;
            margin-top: 5px;
            display: inline-block;
        }
    </style>
</head>
<body>
    <button id="theme-toggle" class="theme-toggle" title="切换主题">
        <i class="fas fa-moon"></i>
    </button>

    <button id="settings-btn" class="settings-btn" title="设置分类">
        <i class="fas fa-sliders-h"></i>
    </button>

    <div class="container">
        <header>
            <h1>一言 · 每日一句</h1>
            <p class="subtitle">V1.0</p>
        </header>
             <main>
            <div id="hitokoto-card" class="card animated">
                <p id="hitokoto-text" class="hitokoto-text">开发：deepseek</p>
                <div class="hitokoto-details">
                    <span id="hitokoto-type" class="hitokoto-badge">企划</span>
                    <span id="hitokoto-source" class="hitokoto-source">—— zjc</span>
                </div>
            </div>
            
            <div id="loader" class="loader" style="display: none;">
                <div class="loader-dot"></div>
                <div class="loader-dot"></div>
                <div class="loader-dot"></div>
            </div>
            
            <div class="current-category">
                <span>当前分类：</span>
                <span id="current-category" class="current-category-display">全部</span>
            </div>
            
            <div class="buttons">
                <button id="get-hitokoto" class="btn">
                    <i class="fas fa-redo-alt"></i> 获取新句
                </button>
                <button id="share-button" class="btn btn-outline">
                    <i class="fas fa-share-alt"></i> 分享
                </button>
            </div>
        </main>
        
        <footer>
            <p>完整支持12种分类 </p>
            <p>句子来源于 <a href="https://hitokoto.cn/" target="_blank">Hitokoto API</a> | 代码由 <span class="heart">❤</span> 提供</p>
        </footer>
    </div>

    <div id="settings-modal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">选择句子分类</h3>
                <button class="close-modal">&times;</button>
            </div>
            
            <div class="category-options">
                <div class="category-option active" data-category="all">
                    <i class="fas fa-globe category-icon"></i>
                    <span class="category-label">全部</span>
                </div>
                <div class="category-option" data-category="a">
                    <i class="fas fa-film category-icon"></i>
                    <span class="category-label">动画</span>
                </div>
                <div class="category-option" data-category="b">
                    <i class="fas fa-book category-icon"></i>
                    <span class="category-label">漫画</span>
                </div>
                <div class="category-option" data-category="c">
                    <i class="fas fa-gamepad category-icon"></i>
                    <span class="category-label">游戏</span>
                </div>
                <div class="category-option" data-category="d">
                    <i class="fas fa-book-open category-icon"></i>
                    <span class="category-label">文学</span>
                </div>
                <div class="category-option" data-category="e">
                    <i class="fas fa-feather-alt category-icon"></i>
                    <span class="category-label">原创</span>
                </div>
                <div class="category-option" data-category="f">
                    <i class="fas fa-network-wired category-icon"></i>
                    <span class="category-label">网络</span>
                </div>
                <div class="category-option" data-category="g">
                    <i class="fas fa-ellipsis-h category-icon"></i>
                    <span class="category-label">其他</span>
                </div>
                <div class="category-option" data-category="h">
                    <i class="fas fa-tv category-icon"></i>
                    <span class="category-label">影视</span>
                </div>
                <div class="category-option" data-category="i">
                    <i class="fas fa-feather category-icon"></i>
                    <span class="category-label">诗词</span>
                </div>
                <div class="category-option" data-category="j">
                    <i class="fas fa-music category-icon"></i>
                    <span class="category-label">网易云</span>
                </div>
                <div class="category-option" data-category="k">
                    <i class="fas fa-brain category-icon"></i>
                    <span class="category-label">哲学</span>
                </div>
                <div class="category-option" data-category="l">
                    <i class="fas fa-smile-wink category-icon"></i>
                    <span class="category-label">抖机灵</span>
                </div>
            </div>
            
            <button id="save-settings" class="save-settings">保存设置</button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // DOM 元素
            const hitokotoText = document.getElementById('hitokoto-text');
            const hitokotoSource = document.getElementById('hitokoto-source');
            const hitokotoType = document.getElementById('hitokoto-type');
            const getHitokotoBtn = document.getElementById('get-hitokoto');
            const themeToggle = document.getElementById('theme-toggle');
            const settingsBtn = document.getElementById('settings-btn');
            const loader = document.getElementById('loader');
            const hitokotoCard = document.getElementById('hitokoto-card');
            const shareButton = document.getElementById('share-button');
            const settingsModal = document.getElementById('settings-modal');
            const closeModal = document.querySelector('.close-modal');
            const categoryOptions = document.querySelectorAll('.category-option');
            const saveSettingsBtn = document.getElementById('save-settings');
            const currentCategory = document.getElementById('current-category');
            const corsNotice = document.querySelector('.cors-notice');
            
            // 分类配置
            const categoryNames = {
                'a': '动画',
                'b': '漫画',
                'c': '游戏',
                'd': '文学',
                'e': '原创',
                'f': '网络',
                'g': '其他',
                'h': '影视',
                'i': '诗词',
                'j': '网易云',
                'k': '哲学',
                'l': '抖机灵',
                'all': '全部'
            };
            
            // 当前设置
            let currentSettings = {
                category: 'all',
                theme: 'light'
            };
            
            // 处理深色模式下的CORS通知
            function updateCorsNoticeTheme() {
                if (document.body.classList.contains('dark-theme')) {
                    corsNotice.classList.add('dark-theme');
                } else {
                    corsNotice.classList.remove('dark-theme');
                }
            }
            
            // 加载本地设置
            function loadSettings() {
                const savedSettings = localStorage.getItem('hitokotoSettings');
                if (savedSettings) {
                    currentSettings = JSON.parse(savedSettings);
                    
                    // 应用设置
                    document.querySelector(`.category-option[data-category="${currentSettings.category}"]`).classList.add('active');
                    currentCategory.textContent = categoryNames[currentSettings.category] || '全部';
                    
                    if (currentSettings.theme === 'dark') {
                        document.body.classList.add('dark-theme');
                        themeToggle.innerHTML = '<i class="fas fa-sun"></i>';
                        corsNotice.classList.add('dark-theme');
                    } else {
                        corsNotice.classList.remove('dark-theme');
                    }
                }
                updateCorsNoticeTheme();
            }
            
            // 保存设置到本地存储
            function saveSettings() {
                localStorage.setItem('hitokotoSettings', JSON.stringify(currentSettings));
            }
            
            // 主题切换
            themeToggle.addEventListener('click', function() {
                document.body.classList.toggle('dark-theme');
                
                if (document.body.classList.contains('dark-theme')) {
                    themeToggle.innerHTML = '<i class="fas fa-sun"></i>';
                    currentSettings.theme = 'dark';
                } else {
                    themeToggle.innerHTML = '<i class="fas fa-moon"></i>';
                    currentSettings.theme = 'light';
                }
                
                updateCorsNoticeTheme();
                saveSettings();
            });
            
            // 打开设置模态框
            settingsBtn.addEventListener('click', function() {
                settingsModal.style.display = 'flex';
            });
            
            // 关闭设置模态框
            closeModal.addEventListener('click', function() {
                settingsModal.style.display = 'none';
            });
            
            // 点击模态框外部关闭
            window.addEventListener('click', function(e) {
                if (e.target === settingsModal) {
                    settingsModal.style.display = 'none';
                }
            });
            
            // 选择分类
            categoryOptions.forEach(option => {
                option.addEventListener('click', function() {
                    categoryOptions.forEach(opt => opt.classList.remove('active'));
                    this.classList.add('active');
                    currentSettings.category = this.getAttribute('data-category');
                });
            });
            
            // 保存设置
            saveSettingsBtn.addEventListener('click', function() {
                saveSettings();
                settingsModal.style.display = 'none';
                currentCategory.textContent = categoryNames[currentSettings.category] || '全部';
                fetchHitokoto();
            });
            
            // 获取一言
            async function fetchHitokoto() {
                loader.style.display = 'flex';
                hitokotoCard.classList.remove('animated');
                
                // 重置错误状态
                hitokotoText.classList.remove('error');
                
                // 设置API参数
                let apiUrl = 'https://v1.hitokoto.cn/?encode=json';
                if (currentSettings.category !== 'all') {
                    apiUrl += `&c=${currentSettings.category}`;
                }
                
                // 使用CORS代理解决跨域问题
                const proxyUrl = `https://api.codetabs.com/v1/proxy/?quest=${encodeURIComponent(apiUrl)}`;
                
                try {
                    // 使用fetch时设置超时限制
                    const controller = new AbortController();
                    const timeoutId = setTimeout(() => controller.abort(), 5000);
                    
                    const response = await fetch(proxyUrl, {
                        signal: controller.signal
                    });
                    
                    clearTimeout(timeoutId);
                    
                    // 检查响应状态
                    if (!response.ok) {
                        throw new Error(`HTTP错误! 状态码: ${response.status}`);
                    }
                    
                    const data = await response.json();
                    
                    // 应用数据
                    hitokotoText.textContent = data.hitokoto;
                    
                    // 处理来源信息
                    let sourceInfo = '';
                    if (data.from_who) {
                        sourceInfo += `—— ${data.from_who}`;
                    }
                    
                    if (data.from) {
                        if (sourceInfo) {
                            sourceInfo += `《${data.from}》`;
                        } else {
                            sourceInfo += `——《${data.from}》`;
                        }
                    }
                    
                    hitokotoSource.textContent = sourceInfo || '—— 未知来源';
                    hitokotoType.textContent = categoryNames[data.type] || data.type;
                    
                    // 添加动画效果
                    setTimeout(() => {
                        hitokotoCard.classList.add('animated');
                    }, 10);
                    
                    // 保存到本地存储
                    localStorage.setItem('lastHitokoto', JSON.stringify(data));
                    
                } catch (error) {
                    // 错误处理
                    hitokotoText.textContent = '获取失败，请重试';
                    hitokotoText.classList.add('error');
                    
                    if (error.name === 'AbortError') {
                        hitokotoText.textContent = '请求超时，请检查网络连接';
                    } else if (error.message.includes('Failed to fetch') || error.message.includes('CORS')) {
                        hitokotoText.textContent = '跨域请求被阻止，请使用HTTP服务器访问';
                    } else {
                        hitokotoText.textContent = `API请求失败: ${error.message}`;
                    }
                    
                    hitokotoSource.textContent = '';
                    hitokotoType.textContent = '错误';
                    
                    console.error('获取一言失败:', error);
                } finally {
                    loader.style.display = 'none';
                }
            }
            
            // 按钮点击事件
            getHitokotoBtn.addEventListener('click', fetchHitokoto);
            
            // 分享功能
            shareButton.addEventListener('click', function() {
                const text = hitokotoText.textContent;
                const source = hitokotoSource.textContent;
                const shareText = `${text} ${source}`;
                
                if (navigator.share) {
                    navigator.share({
                        title: '一言 | 触动心灵的句子',
                        text: shareText,
                    }).catch(console.error);
                } else {
                    // 如果不支持分享API，则复制到剪贴板
                    navigator.clipboard.writeText(shareText);
                    
                    // 显示提示
                    const originalContent = shareButton.innerHTML;
                    shareButton.innerHTML = '<i class="fas fa-check"></i> 已复制';
                    setTimeout(() => {
                        shareButton.innerHTML = originalContent;
                    }, 2000);
                }
            });
            
            // 初始化页面
            loadSettings();
            
            // 页面加载时检查本地存储
            const lastHitokoto = localStorage.getItem('lastHitokoto');
            if (lastHitokoto) {
                try {
                    const data = JSON.parse(lastHitokoto);
                    hitokotoText.textContent = data.hitokoto;
                    
                    let sourceInfo = '';
                    if (data.from_who) {
                        sourceInfo += `—— ${data.from_who}`;
                    }
                    
                    if (data.from) {
                        if (sourceInfo) {
                            sourceInfo += `《${data.from}》`;
                        } else {
                            sourceInfo += `——《${data.from}》`;
                        }
                    }
                    
                    hitokotoSource.textContent = sourceInfo || '—— 未知来源';
                    hitokotoType.textContent = categoryNames[data.type] || data.type;
                } catch (e) {
                    console.error('解析本地存储失败:', e);
                    fetchHitokoto();
                }
            } else {
                fetchHitokoto();
            }
        });
    </script>
</body>
</html>